import xml.etree.ElementTree as ET
import os

home = "./"
manifest_home = home+ ".repo/"


def get_all_xml():
    manifest_xml_list1 = []
    for child in ET.parse(manifest_home + "manifest.xml").getroot().findall('include'):
        manifest_xml_list1 += [manifest_home + "manifests/" + child.attrib['name']]
    for xml in manifest_xml_list1:
        root = ET.parse(xml).getroot()
        if root.find('include') is not None:
            print("in include")
            manifest_xml_list1.remove(xml)
            for child in root.findall('include'):
                manifest_xml_list1.append(manifest_home + "manifests/" + child.attrib['name'])
    return manifest_xml_list1


def check_all_bundle(manifest_xml_list):
    # <name, path>
    no_bundle_repo_dist = {}
    count = 0
    for xml in manifest_xml_list:
        root = ET.parse(xml).getroot()
        for child in ET.parse(xml).getroot().findall('project'):
            count = count + 1
            name = child.attrib['name']
            path = child.attrib['path']
            file_path = os.path.join(path, "bundle.json")
            if not os.path.isfile(file_path):
                no_bundle_repo_dist[name] = path
    print(count)
    return no_bundle_repo_dist


def run():
    # 获取全部manifest_xml
    manifest_xml_list = get_all_xml()
    print(manifest_xml_list)
    # 遍历XML文件内容
    no_bundle_repo_dist = check_all_bundle(manifest_xml_list)
    print(len(no_bundle_repo_dist))
    print(no_bundle_repo_dist)

if __name__ == '__main__':
    run()